package com.lg.partition21;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/30 9:16
 */
public class P2015二叉苹果树 {

    private static final int N = 110, M = N * 2;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static int[][] f = new int[N][N];
    private static int n, m, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        Arrays.fill(h, -1);
        for (int i = 0; i < n - 1; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            add(a, b, c);
            add(b, a, c);
        }
        dfs(1, -1);
        writer.write(f[1][m] + "\n");
        writer.flush();
    }

    private static void dfs(int u, int parent) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == parent) continue;
            dfs(v, u);
            for (int j = m; j >= 0; j--) {
                for (int k = 0; k < j; k++) {
                    f[u][j] = Math.max(f[u][j], f[u][j - k - 1] + f[v][k] + w[i]);
                }
            }
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }
}
